package service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import bean.Pagination;

import dao.BookDAO;
import dao.CategoryDAO;

import entity.Book;
import entity.Category;
import entity.Product;

import service.MainService;
import util.ObjectFactory;

public class MainServiceImpl implements MainService{
	private CategoryDAO catDao;
	public void setCatDao(CategoryDAO catDao) {
		this.catDao = catDao;
	}
	private BookDAO bookDao;
	public void setBookDao(BookDAO bookDao) {
		this.bookDao = bookDao;
	}
		
	public List<Category> findLeftCategory() throws Exception {
//		CategoryDAO dao = ObjectFactory.getInstance(CategoryDAO.class);
//		List<Category> all = dao.findAll();
////		返回parentId=1的Category对象
//		List<Category> cates = findByParentId(1, all);
//		//加载cats集合元素的子类别
//		for(Category cate : cates){
//			cate.setCatelist(findByParentId(cate.getId(), all));
//		}
		return catDao.findByParentId(1);
	}
	private List<Category> findByParentId(int pid,List<Category> all){
		List<Category> subCates = new ArrayList<Category>();
		for (Category cate : all) {
			if (cate.getParentId()==pid) {
				subCates.add(cate);
			}
		}
		return subCates;
	}
	public Map<Integer,String> findCategoryName(int id)throws Exception{
		Map<Integer,String> cates = new HashMap<Integer,String>();
		cates=catDao.findBYProId(id);
		return cates;
	}
	public Book findBookById(int id)throws Exception{
		return bookDao.findBookById(id);
	}
	public List<Book> findBooks(int page,String value,int cid)throws Exception{
		//设置分页
		List<Book> books = null;
		Pagination p = new Pagination();
		p.setPage(page);
		if(value==null || value==""){
			books=bookDao.findByProperty("add_time","desc",cid,p);
		}else{
			String[] str = value.split(",");
			System.out.println(str[0]);
			System.out.println(str[1]);
			books=bookDao.findByProperty(str[0], str[1], cid, p);
		}
		return books;
	}
	//返回总页数
	public int findTotalPage(int cid)throws Exception{
		Pagination p = new Pagination();
		int totalPage=1;
		int count1=bookDao.findById(cid).size();
		if(count1%p.getPageSize()==0){
			totalPage=count1/p.getPageSize();
		}else{
			totalPage=count1/p.getPageSize()+1;
		}
		return totalPage;
	}
	
	//返回booklist页面左栏类别
	public List<Category> findLeftBookListCategory(int pid)throws Exception{
		List<Category> cates=null;
		cates=catDao.findByParentId(pid);
		return cates;
	}
	//返回booklist页面栏目导航
	public List<String> findNavigation(int cid) throws Exception{
		List<String> cateNames = new ArrayList<String>();
		Category ca = catDao.findById(cid);
		cateNames.add(ca.getName());
		if(ca.getParentId()!=1){
			String name= catDao.findById(ca.getParentId()).getName();
			cateNames.add(0, name);
		}
		return cateNames;
	}
	//设置栏目下产品总数
	public int  getCount(int id)throws Exception{
		//设置总数
		int count=0;
		for(Category c:findLeftBookListCategory(id)){
			count+=c.getPnum();
		}
		return count;
	}



	@Override
	public List<Product> findNew() throws Exception {
		return bookDao.findNew();
	}

	@Override
	public List<Book> findRecommend() throws Exception {
		return bookDao.findRecommend();
	}
	@Override
	public List<Product> findHot(int size) throws Exception {
		return bookDao.findHot(size);
	}
}
